import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Sort {

    /**
     * 选择排序 (单指针)
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void selectSort1(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    public static void swap (int[] array,int i,int index) {
        int tmp = array[index];
        array[index] = array[i];
        array[i] = tmp;

    }

    /**
     * 选择排序 (双指针)
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            if (array[left] == array[maxIndex]) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    /**
     * 堆排序
     * 时间复杂度：O(N*logN)
     * 空间复杂度:O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end != 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    public static void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[] array,int parent,int len) {
        int child = parent*2+1;
        while (child < len) {
            if (child+1 < len && array[child] < array[child + 1]) {
                child = child + 1;
            }
            if (array[child] > array[parent]) {
                swap(array, child, parent);
                parent = child;
                child = parent * 2 + 1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array [j+1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if (flag == false) {
                break;
            }
        }
    }

    /**
     * 快速排序(挖坑法)
     * 时间复杂度：最好情况：O(N*logN)
     *           最坏情况：O(N^2) -> 有序 或者 逆序
     * 空间复杂度：最好情况：logN
     *           最坏情况：N
     * 稳定性：不稳定
     * @param array
     */
    public static void quickSort(int[] array) {
        int left = 0;
        int right = array.length-1;
        quick(array,left,right);
    }

    public static void quick(int[] array,int start,int end) {
        if (start >= end) {
            return;
        }

        //使用此优化，减少递归次数，当数组最后已经快接近有序了，用插入排序
        if ((end-start + 1) < 15) {
            //使用插入排序
            insert(array,start,end);
            return;
        }
        //三数取中法
        int minIndex = midTree(array,start,end);
        swap(array,minIndex,start);

        int povl = partition1(array,start,end);
        quick(array,start,povl-1);
        quick(array,povl+1,end);
    }
    public static int midTree(int[] array,int left,int right) {
        int mid = (left + right)/2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            }else if (array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]) {
                return right;
            }else if (array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    public static int partition1(int[] array,int left,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    /**
     * 快速排序(Hoare法)
     * @param array
     * @param left
     * @param right
     * @return
     */
    public static int partition2(int[] array,int left,int right) {
        int tmp = array[left];
        int start = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,start);
        return left;
    }
    public static void insert(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int j = i-1;
            int tmp = array[i];
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 快速排序(非递归)
     * @param array
     */
    public static void quickSort2(int[] array) {
        Deque<Integer> stack = new LinkedList<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partition2(array,left,right);
        if (pivot > left+1) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if (pivot < right-1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition2(array,left,right);
            if (pivot > left+1) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if (pivot < right-1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度：O(N*logN)
     * 空间复杂度：N
     * 稳定性：稳定
     * @param array
     */
    public static void mergeSort1(int[] array) {
        mergeSortChild(array,0,array.length-1);

    }
    public static void mergeSortChild(int[] array,int left,int right) {
        if (left >= right) {
            return;
        }
        int mid = (left+right)/2;
        mergeSortChild(array,left,mid);
        mergeSortChild(array,mid+1,right);
        merge(array,left,right,mid);
    }
    public static void merge(int[] array,int start,int end,int mid) {
        int s1 = start;
        int s2 = mid+1;
        int[] tmp = new int[end-start+1];
        int k = 0;
        while (s1 <= mid && s2 <= end) {
            if (array[s1] > array[s2]) {
                tmp[k++] = array[s2++];
                //k++;
                //s2++;
            }else {
                tmp[k++] = array[s1++];
                //k++;
                //s1++;
            }
        }
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= end) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i+start] = tmp[i];
        }
    }

    /**
     * 归并排序(非递归实现)
     * @param array
     */
    public static void mergeSort2(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i+= gap*2) {
                int left = i;
                int mid = left+gap-1;
                if (mid >= array.length) {
                    mid = array.length-1;
                }
                int right = left+2*gap-1;
                if (right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }
            gap = gap*2;
        }
    }
}
